﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：入库单管理客户端脚本
//创建人：赵国伟
//生成时间：2012-06-10 16:55:40
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/StoresCommon.aspx";
var ModelOfStockIn;
var ModelOfLine;
var StockInID = 0;
var SelectOptionOfStores;
var SelectOptionOfSupliers;
var SelectOptionOfStatus;
var TableOfLines = "TableOfLines";
var CurRowOfLines = 0;
var CtrlOfSelGoods;

///初始化
function Init() {
    StockInID = 0;
    CurRowOfLines = 0;
}

///执行搜索
function GoToSearch() {
    var url = "StockInsManage.aspx?";
    url += "StockInSn=" + $("#txtStockInSnOfSearch").val();
    url += "&StoreID=" + $("#ddlStoreOfSearch").val();
    url += "&SupplierID=" + $("#ddlSupplierOfSearch").val();
    url += "&StartDate=" + $("#txtStartDateOfSearch").val();
    url += "&EndDate=" + $("#txtEndDateOfSearch").val();
    location.href = url;
}

//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条入库单信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteStockIn&StockInID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#ddlSupplier").val() == "") {
        alert("系统提示：请选择供货商！");
        $("#ddlSupplier").focus();
        return false;
    }
    if ($("#ddlStore").val() == "") {
        alert("系统提示：请选择库房！");
        $("#ddlStore").focus();
        return false;
    }
    if ($("#txtEnterDate").val() == "") {
        alert("系统提示：请输入入库日期！");
        $("#txtEnterDate").focus();
        return false;
    }
    if ($("#txtOrderDate").val() == "") {
        alert("系统提示：请输入采购日期！");
        $("#txtOrderDate").focus();
        return false;
    }
    if ($("#txtBuyerName").val() == "") {
        alert("系统提示：请输入采购员！");
        $("#txtBuyerName").focus();
        return false;
    }
    if ($("#txtBuyerMobile").val() == "") {
        alert("系统提示：请输入采购员联系电话！");
        $("#txtBuyerMobile").focus();
        return false;
    }

    //判断出库清单信息是否填写完整。
    if (CurRowOfLines == 0) {
        alert("系统提示：您还没有设置入库商品！");
        return false;
    }
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null) {
            var goodsName = $("#hfGoodsNameOf" + i).val();


            if ($("#txtPriceOf" + i).val() == "" || !isMoney($("#txtPriceOf" + i).val())) {
                alert("系统提示：" + goodsName + " 的单价输入错误！");
                $("#txtPriceOf" + i).focus();
                return false;
            }
            if (parseFloat($("#txtPriceOf" + i).val()) < 0) {
                alert("系统提示：" + goodsName + "的单价不能小于0！");
                $("#txtPriceOf" + i).focus();
                return false;
            }

            if ($("#txtQuantityOf" + i).val() == "" || !IsNumber($("#txtQuantityOf" + i).val())) {
                alert("系统提示：" + goodsName + " 的数量必须为数字！");
                $("#txtQuantityOf" + i).focus();
                return false;
            }
            if (parseInt($("#txtQuantityOf" + i).val(), 10) < 0) {
                alert("系统提示：" + goodsName + "的数量不能小于0！");
                $("#txtQuantityOf" + i).focus();
                return false;
            }

           
        }
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }
    var data = clone(ModelOfStockIn);
    data = { "InsInfo": { "_buyer": null, "_buyertel": null, "_contractcsn": null, "_createdate": "\/Date(1339324592130+0800)\/", "_enterdate": "\/Date(1339324592130+0800)\/", "_orderdate": "\/Date(1339324592130+0800)\/", "_paymemo": null, "_remark": null, "_status": null, "_stockinid": 0, "_storeid": 0, "_supplierid": 0, "_yunshuyuan": null, "_yunshuyuantel": null }, "Lines": [], "StoreName": null, "SupplierName": null };
    data.InsInfo._stockinid = StockInID;
    data.InsInfo._storeid = $("#ddlStore").val();
    data.InsInfo._supplierid = $("#ddlSupplier").val();
    data.InsInfo._enterdate = "/Date(" + DateStringToUTC($("#txtEnterDate").val()) + ")/";
    data.InsInfo._orderdate = "/Date(" + DateStringToUTC($("#txtOrderDate").val()) + ")/";
    data.InsInfo._buyer = $("#txtBuyerName").val();
    data.InsInfo._buyertel = $("#txtBuyerTel").val();
    data.InsInfo._yunshuyuan = $("#txtYunshuYuan").val();
    data.InsInfo._yunshuyuantel = $("#txtYunshuYuanTel").val();
    data.InsInfo._contractcsn = $("#txtContractCSN").val();
    data.InsInfo._paymemo = $("#txtPayMemo").val();
    data.InsInfo._status = $("#ddlStatus").val();
    data.InsInfo._remark = $("#txtRemark").val();
    data.InsInfo._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";

    //获取订购清单；
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null) {
            var l = clone(ModelOfLine);
            // l = { "GoodsName": null, "LineInfo": { "_goodsid": 0, "_price": 0, "_quantity": 0, "_stocklineid": 0, "_summoney": 0} };
            l.LineInfo._goodsid = $("#hfGoodsIDOf" + i).val();
            l.LineInfo._price = $("#txtPriceOf" + i).val();
            l.LineInfo._quantity = $("#txtQuantityOf" + i).val();
            l.LineInfo._summoney = parseFloat($("#txtPriceOf" + i).val()).Mul(parseInt(l.LineInfo._quantity, 10));
            l.LineInfo._memo = $("#txtMemoOf" + i).val();
            l.GoodsName = $("#hfGoodsNameOf" + i).val();
            data.Lines.push(l);
        }
    }

    var url = AjaxServerUrl + "?Action=SaveStockInInfo";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定入库单信息到录入表单中；
function BindStockInToForm() {
    var url = AjaxServerUrl + "?action=GetStockInInfo&StockInID=" + StockInID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#spStockInID").html(data.InsInfo._stockinid);
                $("#ddlStore").val(data.InsInfo._storeid);
                $("#ddlSupplier").val(data.InsInfo._supplierid);
                $("#txtEnterDate").val(ConvertJsonDatetoToLocateDate(data.InsInfo._enterdate))
                $("#txtOrderDate").val(ConvertJsonDatetoToLocateDate(data.InsInfo._orderdate))
                $("#txtBuyerName").val(data.InsInfo._buyer);
                $("#txtBuyerTel").val(data.InsInfo._buyertel);
                $("#txtYunshuYuan").val(data.InsInfo._yunshuyuan);
                $("#txtYunshuYuanTel").val(data.InsInfo._yunshuyuantel);
                $("#txtContractCSN").val(data.InsInfo._contractcsn);
                $("#txtPayMemo").val(data.InsInfo._paymemo);
                $("#ddlStatus").val(data.InsInfo._status);
                $("#txtRemark").val(data.InsInfo._remark);

                //输出订购清单；
                for (var i = 0; i < data.Lines.length; ++i) {
                    var l = data.Lines[i];
                    AddLine(l.LineInfo._goodsid, l.GoodsName,0, l.PackUnit);
                    $("#txtPriceOf" + i).val(l.LineInfo._price);
                    $("#txtQuantityOf" + i).val(l.LineInfo._quantity);
                    $("#txtMemoOf" + i).val(l.LineInfo._memo);
                }

            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(700, 800, "#e2e2e2", "添加入库单", GetHtmlForm());
    CtrlOfSelGoods = new SelectGoods('btnSelGoods', 'Single', null, null, AddLine);
}

//编辑界面；
function Edit(id) {
    Init();
    StockInID = id;
    GetPopupDiv(700, 800, "#e2e2e2", "编辑入库单", GetHtmlForm());
    CtrlOfSelGoods = new SelectGoods('btnSelGoods', 'Single', null, null, AddLine);
    BindStockInToForm();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"850\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"4\" class=\"HeadTitle\">&gt;&gt;入库单基本信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"103\" align=\"right\">入库单号：</td>");
    html.push("    <td colspan=\"3\"><span id=\"spStockInID\"></span></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">供货商：</td>");
    html.push("    <td width=\"230\"><select name=\"ddlSupplier\" id=\"ddlSupplier\">");
    html.push("      <option value=\"\">--请选择供货商--</option>");
    html.push(SelectOptionOfSupliers);
    html.push("    </select>   <span class=\"red\">(*)</span> </td>");
    html.push("    <td width=\"108\" align=\"right\">库房：</td>");
    html.push("    <td width=\"259\"><select name=\"ddlStore\" id=\"ddlStore\">");
    html.push("      <option value=\"\">--请选择库房--</option>");
    html.push(SelectOptionOfStores);
    html.push("        </select><span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">入库日期：</td>");
    html.push("    <td><input name=\"txtEnterDate\" type=\"text\" id=\"txtEnterDate\" size=\"12\" class=\"Wdate\" readonly=\"readonly\" onfocus=\"new WdatePicker({el:this})\" style=\"cursor: hand\"/><span class=\"red\">(*)</span></td>");
    html.push("    <td align=\"right\">订购日期：</td>");
    html.push("    <td><input name=\"txtOrderDate\" type=\"text\" id=\"txtOrderDate\" size=\"12\" class=\"Wdate\" readonly=\"readonly\" onfocus=\"new WdatePicker({el:this})\" style=\"cursor: hand\" /><span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">采购员：</td>");
    html.push("    <td><input name=\"txtBuyerName\" type=\"text\" id=\"txtBuyerName\" size=\"20\" class=\"border6\" /><span class=\"red\">(*)</span></td>");
    html.push("    <td align=\"right\">采购人电话：</td>");
    html.push("    <td><input name=\"txtBuyerTel\" type=\"text\" id=\"txtBuyerTel\" size=\"20\" class=\"border6\" /><span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">运输员：</td>");
    html.push("    <td><input name=\"txtYunshuYuan\" type=\"text\" id=\"txtYunshuYuan\" size=\"20\" class=\"border6\" /></td>");
    html.push("    <td align=\"right\">运输人联系电话：</td>");
    html.push("    <td><input name=\"txtYunshuYuanTel\" type=\"text\" id=\"txtYunshuYuanTel\" size=\"20\" class=\"border6\" /></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">采购合同编号：</td>");
    html.push("    <td colspan=\"3\"><input name=\"txtContractCSN\" type=\"text\" id=\"txtContractCSN\" size=\"40\" class=\"border6\" /></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">付款说明：</td>");
    html.push("    <td colspan=\"3\"><input name=\"txtPayMemo\" type=\"text\" id=\"txtPayMemo\" size=\"60\" class=\"border6\" /></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">入库状态：</td>");
    html.push("    <td colspan=\"3\"><select name=\"ddlStatus\" id=\"ddlStatus\">");
    html.push(SelectOptionOfStatus);
    html.push("    </select>");
    html.push("    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">备注：</td>");
    html.push("    <td colspan=\"3\"><textarea name=\"txtRemark\" cols=\"70\" rows=\"3\" id=\"txtRemark\"></textarea></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td colspan=\"4\" align=\"center\"><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("      <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("      <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("    </tr>");
    html.push("</table>");
    html.push("<table width=\"850\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"table\" id=\"TableOfLines\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"8\" class=\"HeadTitle\">&gt;&gt;商品明细 <input name=\"btnSelGoods\" type=\"button\" id=\"btnSelGoods\" value=\"选择商品..\" class=\"btn2\"  />  </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"center\">编号</td>");
    html.push("    <td align=\"center\">商品名称</td>");
    html.push("    <td align=\"center\">单位</td>");
    html.push("    <td align=\"center\">单价</td>");
    html.push("    <td align=\"center\">数量</td>");
    html.push("    <td align=\"center\">金额</td>");
    html.push("    <td align=\"center\">备注</td>");
    html.push("    <td align=\"center\">操作</td>");
    html.push("    </tr>");

    html.push("<tr id=\"trNoLine\">");
    html.push("<td colspan=\"8\" class=\"red\" align=\"center\">[ 还没有添加任何商品！ ]</td>");
    html.push("</tr>");

    html.push("  <tr>");
    html.push("    <td colspan=\"4\" align=\"right\">&nbsp;</td>");
    html.push("    <td align=\"center\"><span id=\"spTotalQuantity\">0</span></td>");
    html.push("    <td align=\"center\">￥<span id=\"spTotalMoney\">0</span></td>");
    html.push("    <td align=\"center\">&nbsp;</td>");
    html.push("    <td align=\"center\">&nbsp;</td>");
    html.push("    </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}





function UpdateLineCounts(i) {
    var t = parseInt($("#txtQuantityOf" + i).val(), 10);
    $("#spLineCountsOf" + i).html(parseFloat($("#txtPriceOf" + i).val()).Mul(t));
    UpdateCounts();

}

///更新统计信息；
function UpdateCounts() {
    var totalQuantity = 0;
    var totalMoney = 0.00;
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null && $("#txtQuantityOf" + i) != undefined) {
            var t = parseInt($("#txtQuantityOf" + i).val(), 10);
            totalQuantity += t;
            var s = parseFloat($("#txtPriceOf" + i).val());
            totalMoney += s.Mul(t);
        }
    }
    $("#spTotalQuantity").html(totalQuantity);
    $("#spTotalMoney").html(totalMoney);

}


function AddLine(goodsId, goodsName, saleprice, packUnit) {

    $("#trNoLine").hide();
    var tr = oo(TableOfLines).insertRow(oo(TableOfLines).rows.length - 1);
    td = tr.insertCell(0);
    td.innerHTML = ((CurRowOfLines + 1) + ".");  //序号列；
    td.align = "center";

    td = tr.insertCell(1);
    td.innerHTML = goodsName + "<input type=\"hidden\" name=\"hfGoodsIDOf" + CurRowOfLines + "\" id=\"hfGoodsIDOf" + CurRowOfLines + "\" value=\"" + goodsId + "\" />" + "<input type=\"hidden\" name=\"hfGoodsNameOf" + CurRowOfLines + "\" id=\"hfGoodsNameOf" + CurRowOfLines + "\" value=\"" + goodsName + "\" />";
    td.align = "center";

    td = tr.insertCell(2);
    td.innerHTML = packUnit;
    td.align = "center";

    td = tr.insertCell(3);
    td.innerHTML = "<input name=\"txtPriceOf" + CurRowOfLines + "\" type=\"text\" id=\"txtPriceOf" + CurRowOfLines + "\" size=\"12\" class=\"border6\" value=\"0\" onblur=\"UpdateLineCounts(" + CurRowOfLines + ")\" />";
    td.align = "center";

    td = tr.insertCell(4);
    td.innerHTML = "<input name=\"txtQuantityOf" + CurRowOfLines + "\" type=\"text\" id=\"txtQuantityOf" + CurRowOfLines + "\" size=\"12\" class=\"border6\" value=\"1\" onblur=\"UpdateLineCounts(" + CurRowOfLines + ")\" />";
    td.align = "center";

    td = tr.insertCell(5);
    td.innerHTML = "￥<span id=\"spLineCountsOf" + CurRowOfLines + "\">0</span>";
    td.align = "center";

    td = tr.insertCell(6);
    td.innerHTML = "<input name=\"txtMemoOf" + CurRowOfLines + "\" type=\"text\" id=\"txtMemoOf" + CurRowOfLines + "\" size=\"12\" class=\"border6\" />";
    td.align = "center";


    td = tr.insertCell(7);
    td.innerHTML = "[ <a href=\"javascript:void(0);\" onclick=\"javascript:DeleteLine(this);\" >删除</a> ]";
    td.align = "center";
    CurRowOfLines++;
    UpdateLineCounts(CurRowOfLines - 1);
}

function DeleteLine(objRow) {
    if (!confirm("确定要删除该行吗?"))
        return false;

    var rowIndex = objRow.parentNode.parentNode.rowIndex;
    oo(TableOfLines).deleteRow(rowIndex);
    CurRowOfLines--;
    UpdateCounts();
    if (oo(TableOfLines).rows.length == 4) {
        CurRowOfLines = 0;
        $("#trNoLine").show();
    }

}












